package com.android.example.myapplication;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity4 extends AppCompatActivity {
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        //从布局文件中获取名叫btn_click的按钮控件
        Button btn_click = findViewById(R.id.btn_click);
        //给btn_click设置点击监听器，一旦用户点击按钮，就触发监听器的onClick方法
        btn_click.setOnClickListener(new MyOnClickListener());

//给btn_click设置长点击监听器，一旦用户长按按钮，就触发监听器的onLongClick方法
        btn_click.setOnLongClickListener(new MyOnLongClickListener());


    }
//定义一个点击监听器，它实现了接口View.OnClickListener
    private class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v){//点击事件的处理方法
            if(v.getId() == R.id.btn_click){//判断是否被btn_click点击
                Toast.makeText(MainActivity4.this,"您点击里控件："+ ((TextView)v).getText(),Toast.LENGTH_SHORT).show();
            }
        }
    }

    private class MyOnLongClickListener implements View.OnLongClickListener {
        @Override
        public boolean onLongClick(View v){//长按事件的处理方法
            if(v.getId() == R.id.btn_click){//判断是否被btn_click长按
                Toast.makeText(MainActivity4.this,"您长按了控件："+ ((TextView)v).getText(),Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    }

}